package com.nuanshui.heatedloan.service.cms.security;

import com.alibaba.dubbo.config.annotation.Reference;
import com.nuanshui.heatedloan.constant.GlobalParam;
import com.nuanshui.heatedloan.dal.IdTypeHandler;
import com.nuanshui.heatedloan.dubbo.extension.DubboContextManager;
import com.nuanshui.heatedloan.userinfo.facade.UserQueryService;
import com.nuanshui.heatedloan.userinfo.facade.UserService;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.nuanshui.framework.security.MD5Util;
import com.nuanshui.heatedloan.constant.TransactionManagerType;
import com.nuanshui.heatedloan.userinfo.model.User;

@Service
@Transactional(readOnly = true,value=TransactionManagerType.MANAGER_SERVICE_TRANSACTION_MARGER)
public class FrontUserService implements InitializingBean {

	@Autowired
	private PasswordEncoder pwdEncoder;

	@Reference(url= GlobalParam.USER_DUBBO_SERVER,timeout=GlobalParam.DUBBO_SERVER_TIME_OUT,version = GlobalParam.USER_DUBBO_SERVER_VERSION)
	private UserQueryService userQueryService;

	@Reference(url= GlobalParam.USER_DUBBO_SERVER,timeout=GlobalParam.DUBBO_SERVER_TIME_OUT,version = GlobalParam.USER_DUBBO_SERVER_VERSION)
	private UserService userService;
	
	public User getFrontUserByPhone(String phone){
		return userQueryService.loadUserByPhone(phone);
	}
	
	/**
	 * 获取app端的用户，根据用户id
	 * @author Gonzo
	 * @date 2017年1月12日下午8:15:47
	 * @param userId
	 * @return
	 * @since 1.0.0
	 */
	public User getFrontUserByUserId(String userId){
		return userQueryService.load(userId);
	}

	
	/**
	 * 修改app端的用户
	 * @author Gonzo
	 * @date 2017年1月12日下午8:15:38
	 * @param frontUserId
	 * @param plainPassword
	 * @since 1.0.0
	 */
	@Transactional(readOnly = false,value=TransactionManagerType.MANAGER_SERVICE_TRANSACTION_MARGER)
	public void updatePassWord(Integer frontUserId, String plainPassword) {
		//首先把密码md5加密，再使用pwdEncode加密
		plainPassword = MD5Util.md5(plainPassword).toLowerCase();//得到小写的md5值。
		userService.changePwd(IdTypeHandler.encode(frontUserId),plainPassword);
	}
	
	/**
	 * 修改app端的用户昵称
	 * @author zhousishang
	 * @date 2017年4月28日下午12:38:38
	 * @param frontUserId
	 * @param nick
	 * @since 1.0.0
	 */
	@Transactional(readOnly = false,value=TransactionManagerType.MANAGER_SERVICE_TRANSACTION_MARGER)
	public void updateNick(Integer frontUserId, String nick) {

		User user=new User();
		user.setId(IdTypeHandler.encode(frontUserId));
		user.setNick(nick);
		userService.updateUserInfo(user,false);
	}
	
	/**
	 * 密码验证
	 * @param id
	 * @param oldPassword
	 * @return
	 */
	public boolean checkPassWord(String id,String oldPassword){
		boolean flag=false;
		User u=this.getFrontUserByUserId(id);
		oldPassword = MD5Util.md5(oldPassword).toLowerCase();
		flag =pwdEncoder.matches(oldPassword, u.getPassword());
		return flag;
	}

	@Override
	public void afterPropertiesSet() throws Exception {
		userQueryService= DubboContextManager.getDubboBean(UserQueryService.class,GlobalParam.USER_DUBBO_SERVER_VERSION);
		userService= DubboContextManager.getDubboBean(UserService.class,GlobalParam.USER_DUBBO_SERVER_VERSION);
	}
}
